package com.wangxiaotao.springcloud.controller;


import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.wangxiaotao.springcloud.service.PaymentHystrixService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
@DefaultProperties(defaultFallback = "payment_Global_FallBackMethod")//配置全局默认降级执行方法 没有配置就执行这个方法
public class OrderHystrixController {

    @Autowired
    private PaymentHystrixService paymentHystrixService;

    @GetMapping(value = "/consumer/payment/hystrix/ok/{id}")
    @HystrixCommand//加了此标签 出现fallback 如果没有参数就就找全局的  有参数就找参数设置的方法
    public String paymentInfo_Ok(@PathVariable("id")Integer id){
//        int i=1/0;
        return paymentHystrixService.paymentInfo_Ok(id);
    }

    /**
     * fallback 服务降级 超时 或者出现问题后执行兜底的方法
     * @param id
     * @return
     */
    @GetMapping(value = "/consumer/payment/hystrix/timeout/{id}")
    @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutFallbackMethod",commandProperties =
            {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1000")})
    public String paymentInfo_timeOut(@PathVariable("id")Integer id){
//        int i=10/0;
        try { TimeUnit.MILLISECONDS.sleep(3000); }catch (Exception e){e.printStackTrace();}
        return paymentHystrixService.paymentInfo_timeOut(id);
    }

    public String paymentInfo_TimeOutFallbackMethod(@PathVariable("id")Integer id){
        return "80超时啦，卧槽卧槽卧槽卧槽-->"+"线程池："+Thread.currentThread().getName()+id;
    }

    //flobal fallback 全局配置降级
    public String payment_Global_FallBackMethod(){
        return "出现错误咯 好难受";
    }
}
